//
//  SendGoodsSingleView.m
//  Shengzhuli
//
//  Created by 卡忙科技 on 16/9/5.
//  Copyright © 2016年 kamangkeji. All rights reserved.
//

#import "SendGoodsSingleView.h"
#import <Masonry.h>
#import "MBProgressHUD+MJ.h"

@interface SendGoodsSingleView()

@property (weak, nonatomic) IBOutlet UIView *containView;
@property (weak, nonatomic) IBOutlet UITextField *companyField;
@property (weak, nonatomic) IBOutlet UITextField *singleField;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *containHeightCons;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *containWidthCons;
@property (weak, nonatomic) IBOutlet UILabel *tipLab;
@property (nonatomic,strong) successBlock success;

@end
@implementation SendGoodsSingleView

+ (instancetype)loadSendView
{
    return [[[NSBundle mainBundle]loadNibNamed:NSStringFromClass([SendGoodsSingleView class]) owner:self options:nil]lastObject];
}
- (void)showWithFrame:(CGRect)frame success:(void (^)(NSString *companyStr,NSString *singleStr))success
{
    self.frame = frame;
    UIWindow *keyWin = [UIApplication sharedApplication].keyWindow;
    [keyWin addSubview:self];
    [self animatedShow];
    self.success = success;
}

- (IBAction)actionClick:(UIButton *)btn
{
    if (btn.tag == 0) {
        [self animatedHide];
    }else{
        if (self.companyField.text.length == 0) {
            [MBProgressHUD showOnlyText:@"物流公司不可为空" view:nil];
            return;
        }
        if (self.singleField.text.length == 0) {
            [MBProgressHUD showOnlyText:@"物流单号不可为空" view:nil];
            return;
        }
        if (self.success) {
            self.success(self.companyField.text,self.singleField.text);
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self animatedHide];
            });
        }
    }
}
- (void)animatedShow
{
    self.containWidthCons.constant = 280;
    self.containHeightCons.constant = 170;
    [UIView animateWithDuration:0.25 animations:^{
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        [self.companyField becomeFirstResponder];
    }];
}
- (void)animatedHide
{
    self.containWidthCons.constant = 28;
    self.containHeightCons.constant = 17;
    [UIView animateWithDuration:0.25 animations:^{
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self animatedHide];
}

@end
